package com.yinhai.httpclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
	public String post(String baseUrl) {
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();  
		params.add(new BasicNameValuePair("param1", "Post方法"));  
		params.add(new BasicNameValuePair("param2", "第二个参数"));  
		StringBuffer content = new StringBuffer();        
		try {  
		    HttpPost postMethod = new HttpPost(baseUrl);
		    HttpClient httpClient = new DefaultHttpClient();
		    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中  
		                  
		    HttpResponse response = httpClient.execute(postMethod); //执行POST方法  
		 // 若状态码为200 ok
		    if(200 == response.getStatusLine().getStatusCode()) {
		    	content.append(EntityUtils.toString(response.getEntity(), "utf-8"));
		    }
		}catch(Exception e) {
			e.printStackTrace();
		}
		return content.toString();
	}
}
